//index.js
Page({

    data:{
        playlists:[],
        inputValue:"",
        searchList:[]
    },
    
    onLoad(){
        /* 发送http请求获取数据 */
        let url ="http://localhost:8080/"
        wx.request({
            url,
            
            header: {'content-type':'application/json'},
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: (result)=>{
                let arr = []
                result.data.forEach((item,index)=>{
                    let{id,avatar,username,tag,title,icons,like,favorite} = item;
                    arr.push({
                        id,avatar,username,tag,title,icons,like,favorite
                    })
                })
                console.log(arr);
                this.setData({
                    playlists:arr
                })
            },
        });
    },
    detail: function (e) {
        var that = this
        // console.log(e)//打印数据
        console.log(e.currentTarget.dataset.id)//打印数据
        var id = e.currentTarget.dataset.id
        var listData = JSON.stringify(that.data.playlists[id])
        wx.navigateTo({
            title:"detail",
            url: '/pages/detail/detail?listData=' +listData
        })
    },
    inputBind: function(event) {
        this.setData({
            inputValue: event.detail.value
        })
    },
    query:function(event){
        var that=this
        var playlists = that.data.playlists
        var searchList = []
        var inputValue = that.data.inputValue
        if (!that.data.inputValue) {
            //没有搜索词 友情提示
            wx.showToast({
                title: '请重新输入',
                icon:'none',
                duration: 2000,
            })
        } else {
            //查找包含关键字的内容
            for(var i=0;i<playlists.length;i++){
                var string = playlists[i].title
                if(string.indexOf(that.data.inputValue)>=0){
                    searchList.push(playlists[i])
                }
            }
            wx.navigateTo({
                url: '../search/search?inputValue='+inputValue,
            })   
        }
        //查找内容为空
        if(searchList==0){
            wx.showToast({
                title: '找不到你想要的内容',
                icon:'none',
                duration: 2000,
            })
        }
        this.setData({
            searchList:searchList
        })
        wx.setStorage({//存储到本地
            key:"searchList",
            data:searchList,
          })
    }
  })